package com.jettech.oa.web.admin;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.jettech.oa.domain.FormProperty;
import com.jettech.oa.domain.WorkFlow;
import com.jettech.oa.domain.WorkFlowForm;
import com.jettech.oa.service.WorkFlowService;

@Controller
@RequestMapping(value="/system/workflow/form")
public class WorkFlowFormController {
	@Autowired
	private WorkFlowService workFlowService;
	
	@RequestMapping(value="/list/{id}",method=RequestMethod.GET)
	public String list(@PathVariable Long id,Model model){
		List<WorkFlowForm> list = workFlowService.findWorkFlowFormList(id);
		model.addAttribute("list",list);
		model.addAttribute("workFlowId",id);
		return "system/workflow/form/list";
	}
	
	@RequestMapping(value="/{id}",method=RequestMethod.GET)
	public String toCreate(@PathVariable Long id,Model model){
		model.addAttribute("workFlowId", id);
		return "system/workflow/form/createForm";
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public String create(@ModelAttribute WorkFlowForm workFlowForm,@RequestParam Long workFlowId,HttpServletRequest request){
		if(workFlowId!=null){
			String[] propertyValues = request.getParameterValues("propertyValue");
			if(propertyValues!=null && propertyValues.length>0 ){
				int propertySum = propertyValues.length;
				List<FormProperty> propertys = new ArrayList<FormProperty>();
				for(int i=0;i<propertySum;i++){
					FormProperty property = new FormProperty();
					property.setValue(propertyValues[i]);
					property.setWorkFlowForm(workFlowForm);
					propertys.add(property);
				}
				workFlowForm.setFormProperties(propertys);
			}
			WorkFlow workFlow = new WorkFlow();
			workFlow.setId(workFlowId);
			workFlowForm.setWorkFlow(workFlow);
		}
		workFlowService.saveWorkFlowForm(workFlowForm);
		return "redirect:/system/workflow/form/list/"+workFlowId;
	}
	/**
	 * 查看表单
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/view/{id}", method=RequestMethod.GET)
	public String getView(@PathVariable Long id, Model model) {
		WorkFlowForm workFlowForm = workFlowService.getWorkFlowForm(id);
		if(workFlowForm!=null){
			model.addAttribute("workFlowForm",workFlowForm);
			if(workFlowForm.getWorkFlow()!=null){
				model.addAttribute("workFlowId",workFlowForm.getWorkFlow().getId());
			}
		}
		return "system/workflow/form/view";
	}
	/**
	 * 转到表单信息页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/update/{id}", method=RequestMethod.GET)
	public String toUpdate(@PathVariable Long id, Model model) {
		WorkFlowForm workFlowForm = workFlowService.getWorkFlowForm(id);
		if(workFlowForm!=null){
			model.addAttribute("workFlowForm",workFlowForm);
			if(workFlowForm.getWorkFlow()!=null){
				model.addAttribute("workFlowId",workFlowForm.getWorkFlow().getId());
			}
		}
		return "system/workflow/form/updateForm";
	}
	/**
	 * 更新表单信息
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/update", method=RequestMethod.POST)
	public String update(@ModelAttribute WorkFlowForm workFlowForm,@RequestParam Long workFlowId,HttpServletRequest request) {
		if(workFlowId!=null){
			String[] propertyValues = request.getParameterValues("propertyValue");
			if(propertyValues!=null && propertyValues.length>0 ){
				int propertySum = propertyValues.length;
				List<FormProperty> propertys = new ArrayList<FormProperty>();
				for(int i=0;i<propertySum;i++){
					FormProperty property = new FormProperty();
					property.setValue(propertyValues[i]);
					property.setWorkFlowForm(workFlowForm);
					propertys.add(property);
				}
				workFlowForm.setFormProperties(propertys);
			}
			WorkFlow workFlow = new WorkFlow();
			workFlow.setId(workFlowId);
			workFlowForm.setWorkFlow(workFlow);
		}
		workFlowService.saveWorkFlowForm(workFlowForm);
		request.getSession().setAttribute("operationFlag", "t");
		return "redirect:/system/workflow/form/list/"+workFlowId;
	}
}
